package com.ornament.app.domain;

import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EntityManager;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.Lob;
import javax.persistence.ManyToOne;
import javax.persistence.PersistenceContext;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;

import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.transaction.annotation.Transactional;

@Configurable
@Entity
@Table(name = "image")
public class Image {

    @NotNull
    @Column(name = "image", columnDefinition = "blob")
    @Lob
    private byte[] imageFile;

	public byte[] getImageFile() {
        return this.imageFile;
    }

	public void setImageFile(byte[] imageFile) {
        this.imageFile = imageFile;
    }

	@PersistenceContext
    transient EntityManager entityManager;

	public static final EntityManager entityManager() {
        EntityManager em = new Image().entityManager;
        if (em == null) throw new IllegalStateException("Entity manager has not been injected (is the Spring Aspects JAR configured as an AJC/AJDT aspects library?)");
        return em;
    }

	public static long countImages() {
        return entityManager().createQuery("SELECT COUNT(o) FROM Image o", Long.class).getSingleResult();
    }

	public static List<Image> findAllImages() {
        return entityManager().createQuery("SELECT o FROM Image o", Image.class).getResultList();
    }

	public static Image findImage(Integer id) {
        if (id == null) return null;
        return entityManager().find(Image.class, id);
    }

	public static List<Image> findImageEntries(int firstResult, int maxResults) {
        return entityManager().createQuery("SELECT o FROM Image o", Image.class).setFirstResult(firstResult).setMaxResults(maxResults).getResultList();
    }

	@Transactional
    public void persist() {
        if (this.entityManager == null) this.entityManager = entityManager();
        this.entityManager.persist(this);
    }

	@Transactional
    public void remove() {
        if (this.entityManager == null) this.entityManager = entityManager();
        if (this.entityManager.contains(this)) {
            this.entityManager.remove(this);
        } else {
            Image attached = Image.findImage(this.id);
            this.entityManager.remove(attached);
        }
    }

	@Transactional
    public void flush() {
        if (this.entityManager == null) this.entityManager = entityManager();
        this.entityManager.flush();
    }

	@Transactional
    public void clear() {
        if (this.entityManager == null) this.entityManager = entityManager();
        this.entityManager.clear();
    }

	@Transactional
    public Image merge() {
        if (this.entityManager == null) this.entityManager = entityManager();
        Image merged = this.entityManager.merge(this);
        this.entityManager.flush();
        return merged;
    }

	public String toString() {
        return ReflectionToStringBuilder.toString(this, ToStringStyle.SHORT_PREFIX_STYLE);
    }

	@ManyToOne
    @JoinColumn(name = "album", referencedColumnName = "id", nullable = false)
    private Album album;

	@Column(name = "name", length = 45, unique = true)
    @NotNull
    private String name;

	public Album getAlbum() {
        return album;
    }

	public void setAlbum(Album album) {
        this.album = album;
    }

	public String getName() {
        return name;
    }

	public void setName(String name) {
        this.name = name;
    }

	@Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id")
    private Integer id;

	public Integer getId() {
        return this.id;
    }

	public void setId(Integer id) {
        this.id = id;
    }
}
